import cv2
import numpy as np
import os


class Parameter:
    rootDir = os.getcwd()
    assetRootDir = rootDir + "/asset"
    resultRootDir = rootDir + "/result"


def add_student_id(
    img: np.ndarray,
    student_id: str,
    font_type=cv2.FONT_HERSHEY_SIMPLEX,
    font_size: int = 2,
    font_color: tuple = (0, 0, 0),
    font_thickness: int = 2,
) -> np.ndarray:
    # 设置要写的学号，并且获取文字的大小
    (text_width, text_height), _ = cv2.getTextSize(
        student_id, font_type, font_size, font_thickness
    )
    _, img_width, _ = img.shape
    # 顶部居中
    x = (img_width - text_width) >> 1
    y = text_height + 5
    # 写学号
    cv2.putText(
        img,
        student_id,
        (x, y),
        font_type,
        font_size,
        font_color,
        font_thickness,
        cv2.LINE_AA,
    )
    return img


if __name__ == "__main__":
    # 学号
    student_id = "22122769"
    # 原图
    img_path = Parameter.assetRootDir + "/number.jpg"
    # 结果路径
    result_path = Parameter.resultRootDir + "/task1/number_result.jpg"
    # 读图片
    img = cv2.imread(img_path)
    # 加上学号
    result_img = add_student_id(img, student_id)
    # 保存
    cv2.imwrite(result_path, img)
